package library

import (
	"Family/library/log"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"

    
	"time"
)

var Db *gorm.DB

func DbCon() {
	// 数据库连接信息
	conf := ConfigParam.Database
	dsn := strings.Join([]string{"host=", conf.DbHosts, " user=", conf.DbUser, " password=", conf.DbPassword,
		" dbname=", conf.DbName, " port=", conf.DbPort, " sslmode=disable TimeZone=Asia/Shanghai"}, "")

	db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})

	sqlDB, err := db.DB()
	sqlDB.SetMaxIdleConns(10)
	sqlDB.SetMaxOpenConns(100)
	sqlDB.SetConnMaxLifetime(time.Minute)

	if err != nil {
		log.ErrorF("Database connection failure, error: %s", err)
		panic(err)
	}

	Db = db
}
